package com.qingxin.park.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.ParkBlockService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysGroup;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;


/**
 * 园区-地块的左侧树
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/parkmenu")
public class ParkMenuAction extends BaseAction {
	
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private ParkBlockService parkBlockService;
	@Autowired
	private SysGroupService sysGroupService;
	
	/**
	 * 加载框架左侧园区-地块导航树数据
	 * @param module
	 * @param lefttab
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/menudata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object menudata(String module, String lefttab,
			String pdatatype, String pid, HttpServletRequest request){
		//园区管理
		if("park".equalsIgnoreCase(module)){
			return loadParkModule(pdatatype, pid, request);
		}
//		//设备管理
//		if("device".equalsIgnoreCase(module)){
//		}
		//园区分布地图
		if("map".equalsIgnoreCase(module) || "provenance".equalsIgnoreCase(module)){
			return loadMapModule(pdatatype, pid, request);
		}
		
		return loadCommon(module, lefttab, pdatatype, pid, request);
	}
	
	
	@RequestMapping(value="/groupdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object groupdata(String module, String lefttab,
			String pdatatype, String pid, HttpServletRequest request){
		//园区管理
		if("park".equalsIgnoreCase(module)){
			return loadParkModuleByGroup(pdatatype, pid, request);
		}
//		//设备管理
//		if("device".equalsIgnoreCase(module)){
//		}
		//园区分布地图
		if("map".equalsIgnoreCase(module)){
			return loadMapModuleByGroup(pdatatype, pid, request);
		}
		
		return loadCommonByGroup(module, lefttab, pdatatype, pid, request);
	}
	
	
	
	/**
	 * 园区管理
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<TreeNode> loadParkModule(String pdatatype, String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		LoginUser loginUser = getLoginUser(request);
		
		if(StringUtils.isEmpty(pid)){
			TreeNode root = new TreeNode("0", "所有园区", "GROUP", null, true);
			list.add(root);
		}
		else if("GROUP".equalsIgnoreCase(pdatatype)){
			list.addAll(farmParkService.getUITreeData(null, loginUser, false));
		}
		else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
			//list.addAll(parkBlockService.getTreeData(pid, false, loginUser));
		}
		return list;
	}
	
	
	/**
	 * 园区管理-按分组
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<TreeNode> loadParkModuleByGroup(String pdatatype, String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid)){
			if(SystemCfg.isAdminUser(loginUser)){
				return sysGroupService.getGroupTree("0", "GROUP", null, loginUser);
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					list.add(new TreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true));
				}
				return list;
			}
		}else{
			if("GROUP".equalsIgnoreCase(pdatatype)){
				list.addAll(sysGroupService.getGroupTree(pid, "GROUP", null, loginUser));
				list.addAll(farmParkService.getUITreeData(pid, loginUser, false));
				return list;
			}else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
				//list.addAll(parkBlockService.getUITreeData(pid, false, loginUser));
			}
		}
		return list;
	}
	
	
	/**
	 * 园区分布地图
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<TreeNode> loadMapModule(String pdatatype, String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		LoginUser loginUser = getLoginUser(request);
		
		if(StringUtils.isEmpty(pid)){
			TreeNode root = new TreeNode("0", "所有园区", "GROUP", null, true);
			list.add(root);
		}
		else if("GROUP".equalsIgnoreCase(pdatatype)){
			list.addAll(farmParkService.getUITreeData(null, loginUser, false));
		}
		return list;
	}
	
	
	/**
	 * 园区分布地图-按分组
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<TreeNode> loadMapModuleByGroup(String pdatatype, String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid)){
			if(SystemCfg.isAdminUser(loginUser)){
				return sysGroupService.getGroupTree("0", "GROUP", null, loginUser);
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					list.add(new TreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true));
				}
				return list;
			}
		}else{
			if("GROUP".equalsIgnoreCase(pdatatype)){
				list.addAll(farmParkService.getUITreeData(pid, loginUser, false));
			}
		}
		return list;
	}
	
	
	/**
	 * 通用导航菜单数据
	 * @param module
	 * @param lefttab
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<TreeNode> loadCommon(String module, String lefttab, String pdatatype, String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid)){
			list.addAll(farmParkService.getUITreeData(null, loginUser, true));
		}
		else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
			list.addAll(parkBlockService.getUITreeData(pid, false, loginUser));
		}
		return list;
	}
	
	
	/**
	 * 通用导航菜单数据-按分组
	 * @param module
	 * @param lefttab
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<TreeNode> loadCommonByGroup(String module, String lefttab, String pdatatype, String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid)){
			if(SystemCfg.isAdminUser(loginUser)){
				return sysGroupService.getGroupTree("0", "GROUP", null, loginUser);
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					list.add(new TreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true));
				}
				return list;
			}
		}else{
			if("GROUP".equalsIgnoreCase(pdatatype)){
				list.addAll(sysGroupService.getGroupTree(pid, "GROUP", null, loginUser));
				list.addAll(farmParkService.getUITreeData(pid, loginUser, false));
				return list;
			}else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
				list.addAll(parkBlockService.getUITreeData(pid, false, loginUser));
			}
		}
		return list;
	}
	
	
	
}
